package com.yonyou.gmmc.service.customer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.dms.dto.IfMessage;
import org.marketing.common.dms.dto.ResultCouponMessage;
import org.marketing.common.dms.dto.ResultPointMessage;
import org.marketing.common.exception.ServiceBizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.service.customer.constant.CustomerInfoConstant;
import com.yonyou.gmmc.service.customer.entity.ConsumeCouponPO;
import com.yonyou.gmmc.service.customer.entity.CouponPO;
import com.yonyou.gmmc.service.customer.entity.DeductPointPO;
import com.yonyou.gmmc.service.customer.entity.PointPO;
import com.yonyou.gmmc.service.customer.service.DCMSCustomerService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * @author Administrator
 * @date 2017年3月8日counselors
 */
@RestController
@RequestMapping(value = CustomerInfoConstant.BASE_PATH + "/" + CustomerInfoConstant.VERSION)
@Api("DCMS调用-客户信息API")
public class DCMSCustomerController {
	private Logger logger = Logger.getLogger(CustomerController.class);
	@Autowired
	private DCMSCustomerService dcmscustomerService;
	
	/**
	 * 查询用户积分（DCMS调用）
	 * 
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/IC011", method = RequestMethod.POST)
	@ApiOperation(value = "查询用户积分（DCMS调用）", notes = "查询用户积分（DCMS调用）")
	public ResultPointMessage getPointByMobile(@RequestBody IfMessage<List<PointPO>> info) {
		IfMessage<List<PointPO>> ifMessage = new IfMessage<List<PointPO>>();
		ifMessage.setIfCode(info.getIfCode());
		ifMessage.setDataCnt(info.getDataCnt());
		ifMessage.setOccurtime(info.getOccurtime());
		ifMessage.setTransferNo(info.getTransferNo());
		List<PointPO> listPoint = new ArrayList<PointPO>();
		PointPO pointPo = new PointPO();
		pointPo.setMobile(info.getIfData().get(0).getMobile());
		listPoint.add(pointPo);
		ifMessage.setIfData(listPoint);
		logger.info("业务开始" + info);
		ResultPointMessage message = dcmscustomerService.getPointByMobile(ifMessage);
		logger.info("业务结束");
		return message;
	}
	
	/**
	 * 用户扣减积分（DCMS调用）
	 * 
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/IC012", method = RequestMethod.POST)
	@ApiOperation(value = "用户扣减积分（DCMS调用）", notes = "用户扣减积分（DCMS调用）")
	public ResultPointMessage deductPointByMobile(@RequestBody IfMessage<List<DeductPointPO>> info) throws Exception {
		try {
			// logger.info("业务开始：" + info);
			IfMessage<List<DeductPointPO>> ifMessage = new IfMessage<List<DeductPointPO>>();
			ifMessage.setDataCnt(info.getDataCnt());
			ifMessage.setIfCode(info.getIfCode());
			ifMessage.setIfData(info.getIfData());
			ifMessage.setOccurtime(info.getOccurtime());
			ifMessage.setTransferNo(info.getTransferNo());
			ResultPointMessage message = dcmscustomerService.checkDeductPoint(ifMessage);
			logger.info("业务结束");
			return message;
		} catch (Exception e) {
			logger.error("Exception:", e);
			throw new ServiceBizException(e.getMessage(), e);
		}
	}
	
	/**
	 * 查询有效优惠券（IC015）
	 * 
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/IC015", method = RequestMethod.POST)
	@ApiOperation(value = "查询有效优惠券（IC015）", notes = "查询有效优惠券（IC015）")
	public ResultCouponMessage queryCoupon(@RequestBody IfMessage<List<CouponPO>> info) {
		try {
			logger.info("查询有效优惠券业务开始：" + info);
			IfMessage<List<CouponPO>> ifMessage = new IfMessage<List<CouponPO>>();
			ifMessage.setDataCnt(info.getDataCnt());
			ifMessage.setIfCode(info.getIfCode());
			ifMessage.setIfData(info.getIfData());
			ifMessage.setOccurtime(info.getOccurtime());
			ifMessage.setTransferNo(info.getTransferNo());
			ResultCouponMessage message = dcmscustomerService.queryCoupon(ifMessage);
			logger.info("查询有效优惠券业务结束");
			return message;
		} catch (Exception e) {
			logger.error("Exception:", e);
			throw new ServiceBizException(e.getMessage(), e);
		}
	}
	/**
	 * 优惠券核销（IC016）
	 * 
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/IC016", method = RequestMethod.POST)
	@ApiOperation(value = "优惠券核销（IC016）", notes = "优惠券核销（IC016）")
	public ResultCouponMessage consumeCoupon(@RequestBody IfMessage<List<ConsumeCouponPO>> info) {
		logger.info("优惠券核销业务开始：" + info);
		IfMessage<List<ConsumeCouponPO>> ifMessage = new IfMessage<List<ConsumeCouponPO>>();
		ifMessage.setDataCnt(info.getDataCnt());
		ifMessage.setIfCode(info.getIfCode());
		ifMessage.setIfData(info.getIfData());
		ifMessage.setOccurtime(info.getOccurtime());
		ifMessage.setTransferNo(info.getTransferNo());
		ResultCouponMessage message = dcmscustomerService.checkConsumeCoupon(ifMessage);
		logger.info("优惠券核销业务结束");
		return message;
	}
	

	@RequestMapping(value = "/dealerSynchronous", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "经销商信息同步", notes = "经销商信息同步")
	public Map<String, Object> dealerSynchronous() throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = dcmscustomerService.dealerSynchronous();
			return resultMap;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ServiceBizException(e.getMessage(), e);
		}
	}

	@RequestMapping(value = "/dealerUsersSynchronous", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "经销商用户信息同步", notes = "经销商用户信息同步")
	public Map<String, Object> dealerUsersSynchronous() throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			logger.info("经销商用户信息同步开始");
			resultMap = dcmscustomerService.dealerUsersSynchronous();
			logger.info("经销商用户信息同步结束");
			return resultMap;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ServiceBizException(e.getMessage(), e);
		}
	}
}
